//
//  UIImageView+ImageWithURL.m
//  Go!
//
//  Created by 凌風 on 16/7/12.
//  Copyright © 2016年 凌风. All rights reserved.
//

#import "UIImageView+ImageWithURL.h"

@implementation UIImageView (ImageWithURL)

- (void)setImageWithURL:(NSString *)url placeHolderImage:(UIImage *)image {
    if (image) {
        [self setImage:image];
    }
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionTask *task = [session downloadTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       
        if (error == nil) {
            NSData *data = [NSData dataWithContentsOfURL:location];
            
            UIImage *image = [UIImage imageWithData:data];
            
            dispatch_async(dispatch_get_main_queue(), ^{
               
                self.image = image;
                
            });
        }
        
    }];
    
    [task resume];
    
}

@end
